﻿using System;
using NHibernate;
using NUnit.Framework;
using ScrumBox.DataAccess.Repositories;
using ScrumBox.IntegrationTests.DataAccess;
using ScrumBox.Model;

namespace ScrumBox.IntegrationTests
{
    [TestFixture]
    public class ProjectRepositoryTester : DatabaseTesterBase
    {
        [Test]
        public void CanSaveProject()
        {
            var project = new Project("My Project", "MyProject", DateTime.Now);
            var repository = new ProjectRepository(_sessionBuilder);

            repository.Save(project);
            repository.Flush();

            using (var session = _sessionBuilder.GetSession())
            {
                session.Clear();
                var projectToEdit = session.Get<Project>(project.Id);

                Assert.That(projectToEdit.Id == project.Id, "Id should be the same.");
                Assert.That(projectToEdit.Key == project.Key, "Project key should be the same.");
                //TODO: Check for a better way to compare the dates.
                //Assert.That(projectToEdit.StartDate.Value.CompareTo(project.StartDate) == 0, "Start date should be the same.");
                Assert.That(
                    projectToEdit.StartDate.Value.ToLongDateString() == project.StartDate.Value.ToLongDateString(),
                    "Start date should be the same.");
            }
        }

        [Test]
        public void CanSaveProductBackLogItem()
        {
            var project = new Project("My Project", "MyProject", DateTime.Now);
            var productBackLogItem = new ProductBackLogItem("MyBacklogItemStoryEtc",
                "What an amazing short, independent, testable story"){Project = project};

            project.AddProductBackLogItem(productBackLogItem);

            var repository = new ProjectRepository(_sessionBuilder);

            repository.Save(project);
            repository.Flush();

            using (var session = _sessionBuilder.GetSession())
            {
                session.Clear();
                var productBackLogItemToEdit = session.Get<ProductBackLogItem>(productBackLogItem.Id);

                Assert.That(productBackLogItemToEdit.Id == productBackLogItem.Id, "Id should be the same.");
                Assert.That(productBackLogItemToEdit.Title == productBackLogItem.Title, "Title should be the same.");
                Assert.That(productBackLogItemToEdit.Description == productBackLogItem.Description, "Description should be the same.");
            }
        }

        [Test]
        public void CanSaveProjectSprint()
        {
            var project = new Project("My Project", "MyProject", DateTime.Now);

            var sprintStart = DateTime.Now;
            var sprintEnd = DateTime.Now + TimeSpan.FromDays(14);
            var sprintTitle = "MyNewSprint";
            var sprintKey = string.Format("{0}-{1}", project.Key, sprintTitle);
            var sprint = new Sprint(sprintKey, sprintTitle, sprintStart, sprintEnd);

            project.AddSprint(sprint);

            var repository = new ProjectRepository(_sessionBuilder);

            repository.Save(project);
            repository.Flush();

            using (var session = _sessionBuilder.GetSession())
            {
                session.Clear();
                var sprintToEdit = session.Get<Sprint>(sprint.Id);

                Assert.That(sprintToEdit.Id == sprint.Id, "Id should be the same.");
                Assert.That(sprintToEdit.Title == sprint.Title, "Title should be the same.");
            }
        }
    }
}

/*

private MockRepository _mocks;
private ISessionBuilder sessionBuilder;
private ISession session;

[SetUp]
public void Setup()
{
    _mocks = new MockRepository();
}
 
sessionBuilder = _mocks.StrictMock<ISessionBuilder>();
session = _mocks.StrictMock<ISession>();

var project = new Project("My Project", "MyProject", DateTime.Now);

using (_mocks.Record())
{
    Expect.Call(() => sessionBuilder.GetSession()).Return(session);
    Expect.Call(() => session.SaveOrUpdate(project));
    Expect.Call(() => session.Flush());
}

using (_mocks.Playback())
{
    var repository = new ProjectRepository(sessionBuilder);
    repository.Save(project);
}

*/